package com.bfly.mall.admin.service.impl;

import com.bfly.core.base.service.BaseServiceImpl;
import com.bfly.core.context.event.MenuReloadEvent;
import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.mall.admin.dao.MenuMapper;
import com.bfly.mall.admin.entity.Menu;
import com.bfly.mall.admin.dto.MenuFormDTO;
import com.bfly.mall.admin.dto.MenuListDTO;
import com.bfly.mall.admin.service.IMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 长沙商脉科技有限公司
 * @date 2019/7/12 18:38
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
public class MenuServiceImpl extends BaseServiceImpl<Menu, Integer> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private ApplicationContext context;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(Integer... ids) {
        for (int menuId : ids) {
            Menu menu = get(menuId);
            List<Menu> children = menu.getChildren();
            if (children != null) {
                Integer[] childrenIds = new Integer[children.size()];
                for (int i = 0; i < children.size(); i++) {
                    childrenIds[i] = children.get(i).getId();
                }
                remove(childrenIds);
            }

            //解绑系统菜单和角色的关系
            menuMapper.clearRoleMenu(menuId);
        }
        getCache().clear();

        reloadMenuToCache();
        return super.remove(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(MenuFormDTO formDTO) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(formDTO, menu);

        menu.setCreateDate(new Date());
        super.save(menu);
        context.publishEvent(new MenuReloadEvent(context));
        reloadMenuToCache();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(MenuFormDTO formDTO) {
        Menu menu = super.get(formDTO.getId());
        if (menu == null) {
            throw new ServiceResponseException(SysError.MISSING_PARAM, "不存在的菜单信息!");
        }

        BeanUtils.copyProperties(formDTO, menu);
        menu.setUpdateDate(new Date());
        super.edit(menu);
        reloadMenuToCache();
    }

    @Override
    public List<MenuListDTO> getFunctionMenuByAdmin(int adminId) {
        return menuMapper.getFunctionMenuByAdmin(adminId);
    }

    @Override
    public List<MenuListDTO> getFunctionMenuByRole(int roleId) {
        return menuMapper.getFunctionMenuByRole(roleId);
    }

    @Override
    public List<MenuListDTO> getFunctionMenu() {
        return menuMapper.getFunctionMenu();
    }

    /**
     * 触发重新加载缓存
     * @author 长沙商脉科技有限公司
     * @date 2020/10/16 13:50
     */
    private void reloadMenuToCache(){

        context.publishEvent(new MenuReloadEvent(context));
    }
}
